﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using BLL.Order;
using System.Text;
using System.Web.SessionState;

namespace HQWeb.View
{
    /// <summary>
    /// GetReminder 的摘要说明
    /// </summary>
    public class GetReminder : IHttpHandler, IReadOnlySessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            if (context.Session["USER_ID"] != null)
            {
                string strType = context.Request["type"];
                string orderID = context.Request["id"];
                string strUserID = context.Session["USER_ID"].ToString();
                if (strType == "contact")
                {
                    context.Response.Write(GetContact(orderID));
                }
                else if (strType == "payment")
                {
                    context.Response.Write(GetPayment(orderID));
                }
                else if (strType == "goods")
                {
                    context.Response.Write(GetGoods(orderID));
                }
                else if (strType == "savecontact")
                {
                    BLL.Order.T_PUB_ORDER_CONTANCT bll = new BLL.Order.T_PUB_ORDER_CONTANCT();
                    Model.Order.T_PUB_ORDER_CONTANCT model = new Model.Order.T_PUB_ORDER_CONTANCT();
                    model.Order_ID = orderID;
                    model.DateTime = DateTime.Now;
                    model.Contents = context.Request["contactcontent"];
                    model.Other = context.Request["remark"];
                    model.UserID = Convert.ToInt32(strUserID);
                    model.Customer = context.Request["customer"];
                    bll.Add(model);
                    context.Response.Write("");
                }
                else if (strType == "savepayment")
                {
                    Model.Order.T_PUB_ORDER model = new Model.Order.T_PUB_ORDER();
                    T_PUB_ORDER BLL = new T_PUB_ORDER();
                    model = BLL.GetModelByID(orderID);
                    decimal money;
                    string strMoney = context.Request["money"];
                    decimal.TryParse(strMoney, out money);
                    string strOther = context.Request["remark"];
                    BLL.UpdateMoney(money, orderID, strUserID, strOther);
                    context.Response.Write("");
                }
                else if (strType == "savegoods")
                {
                    Model.Order.T_PUB_ORDER model2 = new Model.Order.T_PUB_ORDER();
                    T_PUB_ORDER modell = new T_PUB_ORDER();
                    model2 = modell.GetModelByID(orderID);
                    string checkItems = "";
                    if (context.Request["CDS"] == "1")
                    {
                        checkItems += "1";
                    }
                    else
                    {
                        checkItems += "0";
                    }
                    if (context.Request["Camera"] == "1")
                    {
                        checkItems += "1";
                    }
                    else
                    {
                        checkItems += "0";
                    }
                    if (context.Request["Goods"] == "1")
                    {
                        checkItems += "1";
                    }
                    else
                    {
                        checkItems += "0";
                    }

                    model2.CheckItem = checkItems;
                    modell.Update(model2);
                    context.Response.Write("");
                }
            }
            else
            {
                context.Response.Write("SessionTimeout");
            }
        }

        private string GetContact(string orderID)
        {
            StringBuilder strHTML = new StringBuilder();
            Model.Order.T_PUB_ORDER model2 = new Model.Order.T_PUB_ORDER();
            T_PUB_ORDER modell = new T_PUB_ORDER();
            model2 = modell.GetModelByID(orderID);
            Model.View.T_PUB_CUSTOMERINFO model3 = new Model.View.T_PUB_CUSTOMERINFO();
            BLL.View.T_PUB_CUSTOMERINFO CUSTOMERINFO_BLL = new BLL.View.T_PUB_CUSTOMERINFO();
            model3 = CUSTOMERINFO_BLL.GetModel(model2.CUSTOMER_ID);
            strHTML.Append("<table class=\"tableclass\" align=\"center\" width=\"420\">");
            strHTML.Append("<tr class=\"trclass\"><td class=\"title\" colspan=\"4\">跟踪记录</td></tr>");
            strHTML.Append("<tr class=\"trclass\"><td class=\"tdclass\" height=\"30px\" align=\"right\" style=\"width: 20%\">新郎姓名：</td>");
            strHTML.Append(string.Format("<td class=\"tdclass\" style=\"width: 30%\">{0}<span</td>",model3.GROOM_NAME));
            strHTML.Append("<td class=\"tdclass\" align=\"right\" style=\"width: 20%\">新郎电话：</td>");
            strHTML.Append(string.Format("<td class=\"tdclass\" style=\"width: 30%\">{0}<span</td></tr>", model3.GROOMPHONE));
            strHTML.Append("<tr class=\"trclass\"><td class=\"tdclass\" height=\"30px\" align=\"right\" style=\"width: 20%\">新娘姓名：</td>");
            strHTML.Append(string.Format("<td class=\"tdclass\" style=\"width: 30%\">{0}<span</td>", model3.BRIDE_NAME));
            strHTML.Append("<td class=\"tdclass\" align=\"right\" style=\"width: 20%\">新郎电话：</td>");
            strHTML.Append(string.Format("<td class=\"tdclass\" style=\"width: 30%\">{0}<span</td></tr>", model3.BRIDEPHONE));
            strHTML.Append("<tr class=\"trclass\"><td class=\"tdclass\" height=\"30px\" align=\"right\" style=\"width: 20%\">婚礼时间：</td>");
            strHTML.Append(string.Format("<td class=\"tdclass\" colspan=\"3\">{0}</td></tr>", Convert.ToDateTime(model3.TIME).ToShortDateString()));
            strHTML.Append("<tr class=\"trclass\"><td class=\"tdclass\" height=\"30px\" valign=\"middle\" align=\"right\">内容：</td>");
            strHTML.Append("<td class=\"tdclass\" colspan=\"3\"><textarea id=\"neirong\"></textarea></td></tr>");
            strHTML.Append("<tr class=\"trclass\"><td class=\"tdclass\" height=\"30px\" valign=\"middle\" align=\"right\">备注：</td>");
            strHTML.Append("<td class=\"tdclass\" colspan=\"3\"><textarea id=\"beizhu\"></textarea></td></tr>");
            strHTML.Append("<tr class=\"trclass\"><td class=\"tdclass\" colspan=\"4\" align=\"center\"><input type=\"button\" value=\"保存\" id='addcontact' onclick=\"SaveContact()\"/></td></tr></table>");
            strHTML.Append(string.Format("<div style=\"display:none\" ><input type=\"text\" id=\"contactOrderID\" value=\"{0}\" /><input type=\"text\" id=\"contactCustomerID\" value=\"{1}\" /></div>",model2.ORDER_ID,model3.CUSTOMER_ID));
            return strHTML.ToString();
        }

        private string GetPayment(string OrderID)
        {
            StringBuilder strHTML = new StringBuilder();
            Model.Order.T_PUB_ORDER model = new Model.Order.T_PUB_ORDER();
            T_PUB_ORDER BLL = new T_PUB_ORDER();
            model = BLL.GetModelByID(OrderID);
            strHTML.Append("<table class=\"tableclass\" align=\"center\" width=\"420\"><tr class=\"trclass\"><td class=\"title\" colspan=\"2\" align=\"center\">交款明细</td></tr>");
            strHTML.Append("<tr class=\"trclass\"><td class=\"tdclass\" style=\"width:20%\" align=\"right\">应交金额：</td>");
            strHTML.Append(string.Format("<td class=\"tdclass\" style=\"width:30%\">{0}</td></tr>", model.RealToal));
            strHTML.Append("<tr class=\"trclass\"><td class=\"tdclass\" style=\"width:20%\" align=\"right\">已付金额：</td>");
            strHTML.Append(string.Format("<td class=\"tdclass\">{0}</td></tr>", model.Total));
            strHTML.Append(" <tr class=\"trclass\"><td class=\"tdclass\" align=\"right\">未付金额：</td>");
            strHTML.Append(string.Format("<td class=\"tdclass\">{0}</td></tr>", (model.RealToal - model.Total).ToString()));
            strHTML.Append("<tr class=\"trclass\"><td class=\"tdclass\" align=\"right\">此次交款：</td>");
            strHTML.Append("<td class=\"tdclass\"><input type=\"text\" id=\"textPayment\"/></td></tr>");
            strHTML.Append("<tr class=\"trclass\"><td class=\"tdclass\" height=\"30px\" valign=\"middle\" align=\"right\">备注：</td>");
            strHTML.Append("<td class=\"tdclass\"><textarea id=\"textRemark\"></textarea></td></tr>");
            strHTML.Append("<tr class=\"trclass\"><td class=\"tdclass\" colspan=\"2\" align=\"center\"><input type=\"button\" value=\"保存\" id='addpayment' onclick=\"SavePayment()\"/></td></tr></table>");
            strHTML.Append(string.Format("<div style=\"display:none\" ><input type=\"text\" id=\"paymentOrderID\" value=\"{0}\" /></div>", model.ORDER_ID));
            return strHTML.ToString();
        }

        private string GetGoods(string OrderID)
        {
            StringBuilder strHTML = new StringBuilder();
            Model.Order.T_PUB_ORDER model2 = new Model.Order.T_PUB_ORDER();
            T_PUB_ORDER modell = new T_PUB_ORDER();
            model2 = modell.GetModelByID(OrderID);
            Model.View.T_PUB_CUSTOMERINFO model3 = new Model.View.T_PUB_CUSTOMERINFO();
            BLL.View.T_PUB_CUSTOMERINFO CUSTOMERINFO_BLL = new BLL.View.T_PUB_CUSTOMERINFO();
            model3 = CUSTOMERINFO_BLL.GetModel(model2.CUSTOMER_ID);
            strHTML.Append("<table class=\"tableclass\" align=\"center\" width=\"420\">");
            strHTML.Append("<tr class=\"trclass\"><td class=\"title\" colspan=\"4\">物品交接</td></tr>");
            strHTML.Append("<tr class=\"trclass\"><td class=\"tdclass\" height=\"30px\" align=\"right\" style=\"width: 20%\">新郎姓名：</td>");
            strHTML.Append(string.Format("<td class=\"tdclass\" style=\"width: 30%\">{0}<span</td>", model3.GROOM_NAME));
            strHTML.Append("<td class=\"tdclass\" align=\"right\" style=\"width: 20%\">新郎电话：</td>");
            strHTML.Append(string.Format("<td class=\"tdclass\" style=\"width: 30%\">{0}<span</td></tr>", model3.GROOMPHONE));
            strHTML.Append("<tr class=\"trclass\"><td class=\"tdclass\" height=\"30px\" align=\"right\" style=\"width: 20%\">新娘姓名：</td>");
            strHTML.Append(string.Format("<td class=\"tdclass\" style=\"width: 30%\">{0}<span</td>", model3.BRIDE_NAME));
            strHTML.Append("<td class=\"tdclass\" align=\"right\" style=\"width: 20%\">新郎电话：</td>");
            strHTML.Append(string.Format("<td class=\"tdclass\" style=\"width: 30%\">{0}<span</td></tr>", model3.BRIDEPHONE));
            strHTML.Append("<tr class=\"trclass\"><td class=\"tdclass\" height=\"30px\" align=\"right\" style=\"width: 20%\">婚礼时间：</td>");
            strHTML.Append(string.Format("<td class=\"tdclass\" colspan=\"3\">{0}</td></tr>", Convert.ToDateTime(model3.TIME).ToShortDateString()));
            strHTML.Append("<tr class=\"trclass\"><td class=\"tdclass\" height=\"30px\" align=\"right\">物品清单</td>");
            string goodsString = model2.CheckItem;
            if (goodsString != "" && goodsString == "111")
            {
                strHTML.Append("<td class=\"tdclass\" colspan=\"3\"><input type=\"checkbox\" checked=\"checked\" runat=\"server\" id=\"checkAll\" onclick=\"changeAll()\" />全 选</td></tr>");
            }
            else
            {
                strHTML.Append("<td class=\"tdclass\" colspan=\"3\"><input type=\"checkbox\" runat=\"server\" id=\"checkAll\" onclick=\"changeAll()\" />全 选</td></tr>");
            }
            strHTML.Append("<tr class=\"trclass\"><td class=\"tdclass\" height=\"30px\" align=\"right\">&nbsp;</td>");
            if (goodsString != "" && goodsString.ElementAt(0) == '1')
            {
                strHTML.Append("<td class=\"tdclass\"><input type=\"checkbox\" name=\"good\" checked=\"checked\" id=\"checkCDS\" runat=\"server\" />跟拍光碟</td>");
            }
            else
            {
                strHTML.Append("<td class=\"tdclass\"><input type=\"checkbox\" name=\"good\"  id=\"checkCDS\" runat=\"server\" />跟拍光碟</td>");
            }
            if (goodsString != "" && goodsString.ElementAt(1) == '1')
            {
                strHTML.Append("<td class=\"tdclass\"><input checked=\"checked\" name=\"good\"  type=\"checkbox\" id=\"checkCamera\" runat=\"server\" />摄像碟</td>");
            }
            else
            {
                strHTML.Append("<td class=\"tdclass\"><input type=\"checkbox\" name=\"good\"  id=\"checkCamera\" runat=\"server\" />摄像碟</td>");
            }
            if (goodsString != "" && goodsString.ElementAt(2) == '1')
            {
                strHTML.Append("<td class=\"tdclass\"><input type=\"checkbox\" name=\"good\"  checked=\"checked\" id=\"checkGoods\" runat=\"server\" />客人物品</td></tr>");
            }
            else
            {
                strHTML.Append("<td class=\"tdclass\"><input type=\"checkbox\" name=\"good\" id=\"checkGoods\" runat=\"server\" />客人物品</td></tr>");
            }
            strHTML.Append("<tr class=\"trclass\"><td class=\"tdclass\" colspan=\"4\" align=\"center\"><input type=\"button\" value=\"保存\" id='addgoods' onclick=\"SaveGoods('add')\"/></td></tr></table>");
            strHTML.Append(string.Format("<div style=\"display:none\" ><input type=\"text\" id=\"goodsOrderID\" value=\"{0}\" /></div>", model2.ORDER_ID));
            return strHTML.ToString();
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}